var express = require('express');
var router = express.Router();
var requestHandlers = require("./requestHandlers");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'index' , tips: "choose a operation:"});
});

router.get('/lib_offer', function(req, res, next) {
	var lib_offer = requestHandlers.getLibOfferDataDefault()
  	res.render('lib_offer', { title: 'lib_offer', lib_offer: lib_offer });
});

router.get('/lib_campaign', function(req, res, next) {
	var lib_campaign = requestHandlers.getLibCampaignDefault()
  	res.render('lib_campaign', { title: 'lib_campaign', lib_campaign: lib_campaign });
});


router.get('/interface_info_v1', function(req, res, next) {
	var interface_info_v1 = requestHandlers.getInterfaceInfoDefault()
  	res.render('interface_info_v1', { title: 'interface_info_v1', interface_info_v1: interface_info_v1 });
});

router.get('/allData', function(req, res, next) {
	var allData = requestHandlers.getAllDataDataDefault()
  	res.render('allData', { title: 'allData', allData: allData });
});

router.post('/lib_offer', 
function(req, res, next) 
{
	if(req.body.top_offer_id)
	{	
		function onGet(result)
		{
			if(result.selectSuccess && result.body) 
				res.render('lib_offer', { title: 'lib_offer',lib_offer: result.body});
			else
				res.redirect('/lib_offer');
		}
		requestHandlers.getLibOfferDataOfSql(req.body.top_offer_id, onGet)
	}
	else if(req.body.offer_name)
	{	
		function onWrite(result)
		{
			if(result.insertSuccess)
				res.render('index', { title: 'index', tips: result.message});
			else
				res.redirect('/lib_offer');
		}
		requestHandlers.writeLibOfferToSql(req.body, onWrite)
	}
	else 
	{
		res.redirect('/lib_offer');
	}
});

router.post('/lib_campaign', 
function(req, res, next) 
{	
	if(req.body.top_offer_id)
	{	
		function onGet(result)
		{
			if(result.selectSuccess && result.body) 
				res.render('lib_campaign', { title: 'lib_campaign',lib_campaign: result.body});
			else
			{
				console.log('getLibCampaignByLibOffer fail')
				res.redirect('/lib_campaign');
			}
		}
		requestHandlers.getLibCampaignByLibOffer(req.body.top_offer_id, onGet)
	}
	else if(req.body.top_campaign_id)
	{	
		function onGet(result)
		{
			if(result.selectSuccess && result.body) 
				res.render('lib_campaign', { title: 'lib_campaign',lib_campaign: result.body});
			else
			{
				console.log('getLibCampaignDataOfSql fail')
				res.redirect('/lib_campaign');
			}
		}
		requestHandlers.getLibCampaignDataOfSql(req.body.top_campaign_id, onGet)
	}
	else if(req.body.offer_id)
	{	
		function onWrite(result)
		{
			if(result.insertSuccess)
				res.render('index', { title: 'index', tips: result.message});
			else
				res.redirect('/lib_campaign');
		}
		requestHandlers.writeLibCampaignToSql(req.body, onWrite)
	}
	else 
	{
		res.redirect('/lib_campaign');
	}
});


router.post('/interface_info_v1', 
function(req, res, next) 
{	
	if(req.body.top_campaign_id)
	{	
		function onGet(result)
		{
			if(result.selectSuccess && result.body) 
				res.render('interface_info_v1', { title: 'interface_info_v1',interface_info_v1: result.body});
			else
			{
				console.log('getLibCampaignByLibOffer fail')
				res.redirect('/interface_info_v1');
			}
		}
		requestHandlers.getInterfaceInfoByLibCampaign(req.body.top_campaign_id, onGet)
	}
	else if(req.body.top_interface_info_id)
	{	
		function onGet(result)
		{
			if(result.selectSuccess && result.body) 
				res.render('interface_info_v1', { title: 'interface_info_v1',interface_info_v1: result.body});
			else
			{
				console.log('getInterfaceInfoDataOfSql fail')
				res.redirect('/interface_info_v1');
			}
		}
		requestHandlers.getInterfaceInfoDataOfSql(req.body.top_interface_info_id, onGet)
	}
	else if(req.body.campid)
	{	
		function onWrite(result)
		{
			if(result.insertSuccess)
				res.render('index', { title: 'index', tips: result.message});
			else
				res.redirect('/interface_info_v1');
		}
		requestHandlers.writeInterfaceInfoToSql(req.body, onWrite)
	}
	else 
	{
		res.redirect('/interface_info_v1');
	}
});


router.post('/allData', 
function(req, res, next) 
{
	if(req.body.top_offer_id)
	{	
		function onGet(result)
		{
			if(result.selectSuccess && result.body) 
				res.render('allData', { title: 'allData',allData: result.body});
			else
				res.redirect('/allData');
		}
		requestHandlers.getAllDataByLibOffer(req.body.top_offer_id, onGet)
	}
	else if(req.body.offer_name)
	{	
		function onWrite(result)
		{
			var t1 = result.result_libOffer.message
			var t2 = result.result_libCampaign.message
			var t3 = result.result_interfaceInfo.message
			res.render('index', { title: 'index', tips: t1 + '<br>' + t2 + '<br>' + t3});
		}
		requestHandlers.writeAllDataToSql(req.body, onWrite)
	}
	else 
	{
		res.redirect('/allData')
	}
});


module.exports = router;
